using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Xml;

namespace Iori.DictCN {
    public static class Dictionary {
        private static string UrlStringMain = "http://dict.cn/ws.php?q=";
        private static Encoding XMLEncoding = Encoding.GetEncoding("GBK");

        public static WordEC GetChinese(string pWord) {
            HttpWebRequest httpWebRequest = GetRequest(new Uri(UrlStringMain + Uri.EscapeUriString(pWord)));
            HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;

            WordEC retWordEC = new WordEC();

            using (StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), XMLEncoding)) {
                XmlDocument XMLDoc = new XmlDocument();
                XMLDoc.Load(streamReader);
                XmlNode root = XMLDoc.SelectSingleNode("dict");

                retWordEC.Key = root.SelectSingleNode("key").InnerText;
                retWordEC.Audio = GetAudio(new Uri(root.SelectSingleNode("audio").InnerText));

                retWordEC.Pronunciation = '[' + root.SelectSingleNode("pron").InnerText + ']';
                retWordEC.Pronunciation =  root.SelectSingleNode("pron").InnerText;
                retWordEC.Define = root.SelectSingleNode("def").InnerText;

                retWordEC.Sentencs = new List<Sentence>();
                XmlNodeList sentList = root.SelectNodes("sent");
                foreach (XmlNode curNode in sentList) {
                    Sentence curSentence = new Sentence();
                    curSentence.Originality = curNode.SelectSingleNode("orig").InnerText;
                    curSentence.Translation = curNode.SelectSingleNode("trans").InnerText;
                    retWordEC.Sentencs.Add(curSentence);
                }
            }

            httpWebRequest.Abort();
            httpWebResponse.Close();

            return retWordEC;
        }

        public static WordEC Test() {
            WordEC retWordEC = new WordEC();
            using (StreamReader streamReader = new StreamReader("c:\\aaa.xml", XMLEncoding)) {
                XmlDocument XMLDoc = new XmlDocument();
                XMLDoc.Load(streamReader);
                XmlNode root = XMLDoc.SelectSingleNode("dict");

                retWordEC.Key = root.SelectSingleNode("key").InnerText;
                //retWordEC.Audio = GetAudio(new Uri(root.SelectSingleNode("audio").InnerText));

                retWordEC.Pronunciation = '[' + root.SelectSingleNode("pron").InnerText + ']';
                retWordEC.Define = root.SelectSingleNode("def").InnerText;

                retWordEC.Sentencs = new List<Sentence>();



                XmlNodeList sentList = root.SelectNodes("sent");
                foreach (XmlNode curNode in sentList) {
                    Sentence curSentence = new Sentence();
                    curSentence.Originality = curNode.SelectSingleNode("orig").InnerText;
                    curSentence.Translation = curNode.SelectSingleNode("trans").InnerText;
                    retWordEC.Sentencs.Add(curSentence);
                }
            }
            return retWordEC;
        }

        private static HttpWebRequest GetRequest(Uri pUri) {
            HttpWebRequest httpWebRequest = WebRequest.Create(pUri) as HttpWebRequest;
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";
            httpWebRequest.ContentLength = 0;
            httpWebRequest.CookieContainer = new System.Net.CookieContainer();
            httpWebRequest.GetRequestStream().Write(new byte[0], 0, 0);
            return httpWebRequest;
        }

        private static byte[] GetAudio(Uri pUri) {
            HttpWebRequest httpWebRequest = GetRequest(pUri);
            HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            byte[] ret;
            using (Stream stream = httpWebResponse.GetResponseStream()) {
                int byteData;
                List<byte> byteList = new List<byte>();
                while (-1 != (byteData = stream.ReadByte())) {
                    byteList.Add((byte)byteData);
                }
                ret = byteList.ToArray();
            }
            return ret;
        }

        private static void SaveAudioFile(string pAddress, string pFileName) {
            using (WebClient webClient = new WebClient()) {
                webClient.DownloadFile(pAddress, pFileName);
            }
        }
    }
}
